/**********************************************************************
* <pre>
* FILE : UserManager.java
* CLASS : UserManager
*
* AUTHOR : LAX
*
* FUNCTION : 经销商用户查询.
*
*
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE     | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
*         |2009-09-09| LAX  | Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
/**
 * $UserManager,v0.1 2009/09/09  lax  经销商用户查询$
 */

package com.infodms.dms.actions.common;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.interceptor.LogonInterceptor;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.dao.common.CommonDAO;
import com.infodms.dms.dao.sales.dealer.DealerInfoDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TcPosePO;
import com.infodms.dms.po.TcUserPO;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.dms.chana.dao.DeCommonDao;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;

/**
 * Function       :  查询经销商用户
 * @author        :  LAX
 * CreateDate     :  2009-09-09
 * @version       :  0.1
 */
public class UserManager extends BaseDao{
	private static Logger logger = Logger.getLogger(UserManager.class);
	private static final UserManager dao = new  UserManager();
	private DeCommonDao deCommonDao = DeCommonDao.getInstance();
	
	/**
	 * Function       :  用户登录职位选择 
	 * @author        :  witti
	 * CreateDate     :  2009-09-16
	 * @version       :  0.1
	 */
	public void login() throws Exception {
		ActionContext atx = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)atx.getSession().get(Constant.LOGON_USER);
		try {
			List<TcPosePO> poseList = CommonDAO.queryUserPosition(logonUser.getUserId());
			atx.setOutData("poseList", poseList);
 			if(poseList!=null&&poseList.size()==1) {
				atx.setRedirect("/common/MenuShow/menuDisplay.do?deptId="+poseList.get(0).getOrgId()+"&poseId="+poseList.get(0).getPoseId()+"&poseType="+poseList.get(0).getPoseType()+"&poseBusType="+poseList.get(0).getPoseBusType());
			}else {
				atx.setForword("positon");
			}
		}catch (Exception e) {
			BizException e1 = new BizException(atx,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"用户职位信息");
			logger.error(logonUser,e1);
			atx.setException(e1);
		}
	}
	
	/**
	 * Function       :  用户登录显示职位或经销商信息
	 * CreateDate     :  2010-09-28
	 * @version       :  0.1
	 */
	public void getUserInfo () {
		ActionContext atx = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)atx.getSession().get(Constant.LOGON_USER) ;
		try {
			RequestWrapper request = atx.getRequest() ;
			String flag = CommonUtils.checkNull(request.getParamValue("flag")) ;
			String info = "" ;
			String infoA = "" ;
			String isReport = "0";
			if ("0".equals(flag)) {
				info = CommonUtils.checkNull(request.getParamValue("poseId")) ;
				//如果isReport==“1”，说明有“当日销售看板”
				isReport = DealerInfoDao.getInstance().isDayReport(Long.parseLong(info));
				TcPosePO tcp = new TcPosePO() ;
				tcp.setPoseId(Long.parseLong(info)) ;
				List<TcPosePO> infos = dao.select(tcp);
				if (null != infos && infos.size() > 0) {
					infoA = infos.get(0).getPoseName() ;
				}
			} else {
				info = CommonUtils.checkNull(request.getParamValue("dealerId")) ;
				String poseInfo = CommonUtils.checkNull(request.getParamValue("poseId")) ;
				List<Map<String, Object>> infos = DealerInfoDao.getInstance().getDel(info, poseInfo) ;
				if (null != infos && infos.size() > 0) {
					infoA = infos.get(0).get("DEALER_NAME").toString() ;
				}
			}
			
			atx.setOutData("info", infoA) ;
			atx.setOutData("isReport", isReport) ;
		} catch(Exception e) {
			BizException e1 = new BizException(atx,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"用户信息");
			logger.error(logonUser,e1);
			atx.setException(e1);
		}
	}
	/**
	 * Function       :  用户退出系统 
	 * @author        :  witti
	 * CreateDate     :  2009-09-18
	 * @version       :  0.1
	 */
	public void logout() throws Exception {
		ActionContext atx = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)atx.getSession().get(Constant.LOGON_USER);
		try {
			CommonDAO.disableUserOnlineInfo(logonUser.getUserId());
			CommonDAO.logUserOnlineTime(logonUser.getUserId());
			logger.info("用户："+logonUser.getName()+"正常退出");
			String loginType = (String)atx.getSession().get("loginType");
			if(loginType!=null && loginType.equals("dealer")){
				   String userName = (String)atx.getSession().get("backUser");
				   String backUrl = (String)atx.getSession().get("backUrl");
				   atx.setOutData("backUser", userName);
				   atx.setOutData("backUrl", backUrl);
					atx.getSession().remove(Constant.LOGON_USER);
					atx.getSession().invalidate();
					atx.setForword("/trans.jsp");
					return;
			}
			
			
	//		atx.getSession().remove(Constant.LOGON_USER);
			atx.getSession().invalidate();
			
			logonUser = null;
			// 区分内外网进行Redirect
//			if(CommonUtils.isInnerNetwork(atx.getRequest().getRemoteAddr())) {
//				atx.setRedirect(LogonInterceptor.logoutInnerUrl);
//			}else {
			//	atx.setRedirect(LogonInterceptor.logoutUrl);
			atx.setForword(LogonInterceptor.firstPage);
//			}
		}catch (Exception e) {
			BizException e1 = new BizException(atx,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"用户职位信息");
			logger.error(logonUser,e1);
			atx.setException(e1);
		}
	}
	/**
	 * 
	* @Title: getInterUser 
	* @Description: TODO(下端调用上端的方法，用于获取所有接口用户) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void getInterUser() {
		OutputStreamWriter ow = null;
		ActionContext atx = ActionContext.getContext();
		String rpcDealerCode = atx.getRequest().getParamValue("rpcDealerCode");
		logger.info("获取接口用户 dmsCode === " + rpcDealerCode);
		AclUserBean logonUser = (AclUserBean)atx.getSession().get(Constant.LOGON_USER);
		try{
			Map<String, Object> map = deCommonDao.getDcsCompanyCode(rpcDealerCode);
			List<TcUserPO> list = CommonDAO.getInterUser(map.get("DCS_CODE").toString());
			StringBuffer json = new StringBuffer("");
			if(list!=null && list.size()>0){
				json.append("{T_SYS_USER_ROLE:[");
				for(int i=0;i<list.size();i++){
					TcUserPO po = list.get(i);
					json.append("{");
					json.append("USER_ID:'");
					json.append(po.getAcnt());
					json.append("',");
					json.append("LOGIN_NAME:'");
					json.append(po.getAcnt());
					json.append("',");
					json.append("ROLE_ID:'',");
					json.append("ROLE_NAME:''");
					json.append("}");
					if((i+1)!=list.size()){
						json.append(",");
					}
				}
				json.append("]}");
			}else{
				logger.error("找不到接口用户");
				json.append("{T_SYS_USER_ROLE:[]}");
			}
			
			OutputStream out = atx.getResponse().getOutputStream();
			ow = new OutputStreamWriter(out,"GB2312");  
			ow.write(json.toString()); 
			ow.flush();  
			//atx.setForword("/test.jsp");
		}catch (Exception e) {
			BizException e1 = new BizException(atx,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"用户信息");
			logger.error(logonUser,e1);
			atx.setException(e1);
		} finally {
			if (null != ow) {
				try {
					ow.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 
	* @Title: rpcRequest 
	* @Description: TODO(处理所有接口请求) 
	 */
	public void rpcRequest() {
		//空处理,用于下端做公共的登录请求
	}
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}

}
